package com.weijianhuawen.wjblog.common.validator;


import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2025-03-28 15:10
 */
public class PasswordValidator implements ConstraintValidator<Password, String> {
    @Override
    public void initialize(Password constraintAnnotation) {

    }

    @Override
    public boolean isValid(String password, ConstraintValidatorContext constraintValidatorContext) {
        // 验证密码格式，规则：至少包含大写字母，小写字母，数字，特殊字符（@#$%^&*()_+），长度至少为8位，最长为18位 ^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@#$%^&*()_+])[A-Za-z\d@#$%^&*()_+]{8,18}$
        return password != null && password.matches("^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[@#$%^&*()_+])[A-Za-z\\d@#$%^&*()_+]{8,18}$");
    }
}
